import pygame
import math
import random
import import_image
from constants import Constants
from player import Player
from ball import Ball
from block import Block
class Game_engine():
    def __init__(self, screen):
        self.screen = screen
        self.clock = pygame.time.Clock().tick(Constants.CLOCK)
        #setup background
        self.background = pygame.Surface(self.screen.get_size())
        #load background image
        self.background = pygame.image.load(import_image.filepath("background.png"))
        
        self.option = Constants.START_GAME
        #Collide Area
        self.area = screen.get_rect()
        #Player Propities
        self.player = Player(Constants.PLAYER_XPOS, Constants.PLAYER_YPOS)
        self.pos = 0
        #Ball Propieties
        self.ball = Ball()
        #self.vector = (0,0)
        self.ball_moving = False
        #Block Propities
        self.block = Block()
        self.group_of_blocks = pygame.sprite.Group()
        self.load_blocks()

    def run_game(self):
        while self.option != Constants.EXIT_GAME and self.option != Constants.MENU_GAME:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.option = Constants.MENU_GAME
                    if event.key == pygame.K_LEFT:
                        self.pos -= 4
                    if event.key == pygame.K_RIGHT:
                        self.pos += 4
                    if event.key == pygame.K_SPACE:
                        self.ball_moving = True
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_LEFT:
                        self.pos = 0
                    if event.key == pygame.K_RIGHT:
                        self.pos = 0
                elif event.type == pygame.QUIT:
                        self.option = Constants.EXIT_GAME
                        
            self.clock
            self.update_player()
            self.update_ball(self.ball_moving)
            self.update()
            
    def update(self):
        self.screen.blit(self.background, (0, 0))
        #show arkanoid-player
        self.screen.blit(self.player.image,self.player.rect)
        #show ball
        self.screen.blit(self.ball.image, self.ball.rect)
        #show blocks
        self.group_of_blocks.draw(self.screen)
        #verify collide
        self.is_colliding()
        pygame.display.flip()
        pygame.display.update()
        
    def update_player(self):
        self.player.move(self.pos)
        
    def update_ball(self, ball_moving):
        if ball_moving:
            newpos = self.calculatenewpos()
            self.ball.rect = newpos
        else:
            self.ball.rect.centerx = self.player.rect.centerx
            self.ball.rect.bottom = self.player.rect.top
            self.ball.vector = self.random_vector()
            
    def load_blocks(self):
        i = 0
        j = 0
        xpos = 30
        ypos = 40
        while i <= 4:
            while j <= 8:
                block = Block(xpos, ypos)
                self.group_of_blocks.add(block)
                xpos = xpos + 60
                j += 1
            ypos = ypos + 30
            xpos = 30
            j = 0
            i += 1

    def calculatenewpos(self):
        (angle, z) = self.ball.vector
        (dx,dy) = (z*math.cos(angle),z*math.sin(angle))
        return self.ball.rect.move(dx,dy)
        
    def random_vector(self, x = 9, y = 9):
        random_angle = (0.1*(random.randint(x,y)))
        return (random_angle,self.ball.get_velocity())
    
    def is_colliding(self):
        (angle, z) = self.ball.vector
        if self.player.rect.colliderect(self.ball.rect):
            angle = math.pi - angle
            self.ball.vector = (angle, -z)
        if not self.area.collidepoint(self.ball.rect.bottom, self.ball.rect.bottom):
            self.player.set_position(Constants.PLAYER_XPOS, Constants.PLAYER_YPOS)
            self.ball_moving = False
            self.player.set_life(-1)
            if self.player.get_life() <= 0:
                self.option = Constants.EXIT_GAME
        if not self.area.collidepoint(self.ball.rect.top, self.ball.rect.top):
            angle = math.pi - angle
            self.ball.vector = (angle, -z)
        if not self.area.collidepoint(self.ball.rect.left, self.ball.rect.left):
            angle = math.pi - angle
            self.ball.vector = (angle, z)
        if not self.area.collidepoint(self.ball.rect.right, self.ball.rect.right):
            angle = math.pi - angle
            self.ball.vector = (angle, z)
        for x in self.group_of_blocks:
            if self.ball.rect.colliderect(x.rect):
                self.group_of_blocks.remove(x)
                angle = math.pi - angle
                self.ball.vector = (angle, -z)
